package com.yjd.back.settlement;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.settlement.model.ClearDayDetailModel;
import com.yjd.comm.settlement.model.ClearDayPfModel;
import com.yjd.comm.settlement.service.IClearDayDetailService;
import com.yjd.comm.settlement.service.IClearDayPfService;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2017/8/23.
 */

@Scope("prototype")
@Controller
@RequestMapping("/pfClearDay/")
public class ClearDayPfControl extends BaseControl{

    /**
     * 平台日结算列表
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    public String list() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        if(paramMap.get("cleardate1")!=null){
            paramMap.put("start_date",paramMap.get("cleardate1").toString().replace("-",""));
        }
        if(paramMap.get("cleardate2")!=null){
            paramMap.put("end_date",paramMap.get("cleardate2").toString().replace("-",""));
        }
        paramMap.put("sort", "clear_date desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.clearDayPfService.getPageModel(paramMap, DbWREnums.READ),
                ServletUtil.getPageParameterMap(request),
                ServletUtil.getUrl("/pfClearDay/list"));
        List<ClearDayPfModel> list = (List<ClearDayPfModel>) pager.getPagerModel().getDataList();
        for (ClearDayPfModel pfClearDayModel : list) {
            pfClearDayModel.getStrMap().put("borrow_price", NumberUtil.formatNumber(pfClearDayModel.getBorrow_price(), NumberUtil.FORMAT_4));
            pfClearDayModel.getStrMap().put("loan_price", NumberUtil.formatNumber(pfClearDayModel.getLoan_price(), NumberUtil.FORMAT_4));
            pfClearDayModel.getStrMap().put("trade_price", NumberUtil.formatNumber(pfClearDayModel.getTrade_price(), NumberUtil.FORMAT_4));
            pfClearDayModel.getStrMap().put("create_time", DateUtil.timestampToDateString(pfClearDayModel.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }
        request.setAttribute("pager", pager);
        request.setAttribute("param", paramMap);
        return "pfClearDay.list";
    }



    /**
     * 日结订单明细
     * @return
     * @throws Exception
     */
    @RequestMapping("detail")
    public String detail(Long clearDate) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("clear_date",clearDate);
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.clearDayDetailService.getPageModel(paramMap, DbWREnums.READ),
                ServletUtil.getPageParameterMap(request),
                ServletUtil.getUrl("/pfClearDay/detail"));
        List<ClearDayDetailModel> clearList = (List<ClearDayDetailModel>) pager.getPagerModel().getDataList();
        List<Long> orderIdList = FrameUtil.getBeanFieldValues(clearList, "order_id");
        List<Long> orgIdList = FrameUtil.getBeanFieldValues(clearList, "org_id");
        List<Long> userIdList = FrameUtil.getBeanFieldValues(clearList, "user_id");


        Map<Long,OrganizationModel> orgMap = organizationService.getModelMapByIds(orgIdList,DbWREnums.READ);
        Map<Long,OrderModel> orderMap = orderService.getModelMapByIds(orderIdList,DbWREnums.READ);
        Map<Long,UserDataModel> userMap = userDataService.getModelMapByIds(userIdList,DbWREnums.READ);


        for (ClearDayDetailModel clearDayDetailModel : clearList) {
            if(orgMap!=null){
                OrganizationModel organizationModel =  orgMap.get(clearDayDetailModel.getOrg_id());
                if(organizationModel!=null){
                    clearDayDetailModel.getStrMap().put("org_name",organizationModel.getOrg_name());
                }
            }

            if(orderMap!=null){
                OrderModel orderModel = orderMap.get(clearDayDetailModel.getOrder_id());
                if(orderModel!=null){
                    clearDayDetailModel.getStrMap().put("status", StagesStatusEnum.parse(orderModel.getStatus()).getLabel());
                    clearDayDetailModel.getStrMap().put("order_no",orderModel.getOrder_no());
                    clearDayDetailModel.getStrMap().put("order_time",DateUtil.timestampToDateString(orderModel.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
                    clearDayDetailModel.getStrMap().put("confirm_time",DateUtil.timestampToDateString(orderModel.getConfirm_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
                    if(orderModel.getApprove_refund_time()!=null){
                        clearDayDetailModel.getStrMap().put("approve_refund_time",DateUtil.timestampToDateString(orderModel.getApprove_refund_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
                    }
                }
            }

            if(userMap!=null){
                UserDataModel userDataModel = userMap.get(clearDayDetailModel.getUser_id());
                if(userDataModel!=null){
                    clearDayDetailModel.getStrMap().put("user_name",userDataModel.getId_name());
                }
            }
            clearDayDetailModel.getStrMap().put("create_time",DateUtil.timestampToDateString(clearDayDetailModel.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        request.setAttribute("pager", pager);
        request.setAttribute("param", paramMap);
        return "pfClearDay.detail";
    }

    /**
     * 结算
     * @param id
     * @throws Exception
     */
    @RequestMapping(value = "clear")
    public void clear(Long id) throws Exception {
        ClearDayPfModel clearDayModel = this.clearDayPfService.getModelById(id, DbWREnums.READ);
        if(null == clearDayModel){
            FrameUtil.throwBaseException1("结算单不存在！");
        }
        ClearDayPfModel model = new ClearDayPfModel();
        model.setStatus(1);
        model.setId(clearDayModel.getId());
        model.setUpdate_time(FrameUtil.getTime(null));
        clearDayPfService.update(model);
        this.successMsg(ServletUtil.getUrl("/pfClearDay/list"),"结算成功！");
    }

    /**
     * 生成结算单
     * @throws Exception
     */
    @RequestMapping(value = "generate")
    public void generate() throws Exception {
        String startDate = request.getParameter("cleardate1");
        String endDate = request.getParameter("cleardate2");

        if(StringUtil.isEmpty(startDate)){
            FrameUtil.throwBaseException1("开始日期不能为空");
        }
        if(StringUtil.isEmpty(endDate)){
            FrameUtil.throwBaseException1("结束日期不能为空");
        }
        Integer start = Integer.parseInt(startDate.replace("-",""));
        Integer end = Integer.parseInt(endDate.replace("-",""));
        clearDayPfService.generatePfClearDayData(start,end,FrameUtil.getTime(null));
        this.successMsg(ServletUtil.getUrl("/pfClearDay/list"),"结算单已生成！");
    }


    @Resource
    private IClearDayPfService clearDayPfService;

    @Resource
    private IClearDayDetailService clearDayDetailService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IOrderService orderService;

    @Resource
    private IUserDataService userDataService;

}
